
import {ajax} from '../../utils/httpRequest.js'
import {URI_GOODSDETAILS, URI_GETTKK} from '../../api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getUrlParams: {},
    detailsData: {},
    icon: '',
    goods_list: [],
    sessionFrom: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    var sessionFrom = options.platform;
    if (options.md5 && options.md5 != 'null') {
      sessionFrom += '-' + options.md5;
    } else if (options.goodId && options.goodId != 'null') {
      sessionFrom += '-' + options.goodId;
    }
    //console.log('sessionFrom: ' + sessionFrom);
    this.setData({
      goodId: options.goodId,
      getUrlParams: options,
      sessionFrom: sessionFrom
    })
    wx.setNavigationBarTitle({
      title: options.title
    })
    this.get_details_data();
  },

  get_details_data: function () {
    var that = this;
    ajax.get(URI_GOODSDETAILS, {
      goodId: that.data.getUrlParams.goodId,
      md5: that.data.getUrlParams.md5,
      platformType: that.data.getUrlParams.platform,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      console.log(res.data.data);
      that.setData({
        detailsData: res.data.data,
        goods_list: res.data.data.like,
        icon: res.data.data.icon
      })
    })
  },
  goHome() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  buy_click() {
    var that = this;
    console.log(this.data.getUrlParams.platform);
    if (this.data.getUrlParams.platform == 2 || this.data.getUrlParams.platform == 4 || this.data.getUrlParams.platform == 3) {
      wx.setClipboardData({
        data: that.data.detailsData.details.codeUrl,
        success: function (res) {
          wx.showModal({
            title: 'Url复制成功',
            content: `复制成功！打开手机浏览器粘贴进入即可购买`,
            confirmColor: '#ff474f'
          })
          //复制成功
          // wx.getClipboardData({
          //   success: function (res) {
          //     wx.showModal({
          //       title: '复制成功',
          //       content: `复制成功！打开手机浏览器粘贴进入即可购买`,
          //       confirmColor: '#ff474f'
          //     })
          //   }
          // })
        }
      })
    } else if (this.data.getUrlParams.platform == 1) {
      var that = this;
      ajax.get(URI_GETTKK, {
        goodId: that.data.getUrlParams.goodId,
        platformType: that.data.getUrlParams.platform,
        accessToken: wx.getStorageSync('accessToken')
      }, {}, (res) => {
        wx.setClipboardData({
          data: res.data.data,
          success: function (res) {
            //复制成功
            wx.showModal({
              title: '领取成功',
              content: `领取成功！打开手机淘宝即可领券购买`,
              confirmColor: '#ff474f'
            })
            // wx.getClipboardData({
            //   success: function (res) {
            //
            //   }
            // })
          }
        })
        // console.log(res.data.data);
      })
    }
  },
  toSearch() {
    wx.navigateTo({
      url: "../searchResult/searchResult"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (res) {
    if (res.form === 'button') {
      //来自页内转发
      console.log(res.target);
    }
    return {
      title: '择天惠-一个做优惠的小程序',
      path: `pages/details/details?goodId=${this.data.getUrlParams.goodId}&title=${this.data.getUrlParams.title}&md5=${this.data.getUrlParams.md5}&platform=${this.data.getUrlParams.platform}`,
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }
  }
})